package com.zhongge;

import java.util.LinkedList;
import java.util.Queue;
@SuppressWarnings(value = "all")
/**
 * @ClassName MyStack
 * @Description TODO 使用队列实现栈
 * @Author zhongge
 * @Version 1.0
 */
public class MyStack {
    //始终一个队列为空
    Queue<Integer> qu1;
    Queue<Integer> qu2  = new LinkedList<>();

    public MyStack() {
        this.qu1 = new LinkedList<>();
        this.qu2 = new LinkedList<>();
    }

    /**
     * 入栈
     * @param x
     */
    public void push(int x) {
        if (empty()) {
            qu1.offer(x);
            return;
        }
        if (!qu1.isEmpty()) {
            qu1.offer(x);
        }else {
            qu2.offer(x);
        }
    }

    /**
     * 出队
     * @return
     */
    public int pop() {
        if (empty()) {
            return -1;
        }
        if (!qu1.isEmpty()){
            int size = qu1.size();
            while ((size - 1) > 0) {
                qu2.offer(qu1.poll());
                size--;
            }
            return qu1.poll();
        }else {
            int size = qu2.size();
            while ((size - 1) > 0) {
                qu1.offer(qu2.poll());
                size--;
            }
            return qu2.poll();
        }
    }

    public int top() {
        if (empty()) {
            return -1;
        }
        if (!qu1.isEmpty()){
            int size = qu1.size();
            int tmp = 0;
            while (size  > 0) {
                tmp = qu1.poll();
                qu2.offer(tmp);
                size--;
            }
            return tmp;
        }else {
            int size = qu2.size();
            int tmp = 0;
            while (size  > 0) {
                tmp = qu2.poll();
                qu1.offer(tmp);
                size--;
            }
            return tmp;
        }
    }

    public boolean empty() {
        if (qu1.isEmpty() && qu2.isEmpty()) {
            return true;
        }
        return false;
    }
}
